const strategyService = require('../services/strategy');

// 获取策略列表
exports.getStrategies = async (req, res) => {
  try {
    const strategies = await strategyService.getStrategies();
    res.json(strategies);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 获取单个策略
exports.getStrategyById = async (req, res) => {
  try {
    const strategy = await strategyService.getStrategyById(req.params.id);
    if (!strategy) {
      return res.status(404).json({ error: '策略不存在' });
    }
    res.json(strategy);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 创建策略
exports.createStrategy = async (req, res) => {
  try {
    const strategy = await strategyService.createStrategy(req.body);
    res.status(201).json(strategy);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 更新策略
exports.updateStrategy = async (req, res) => {
  try {
    const strategy = await strategyService.updateStrategy(req.params.id, req.body);
    if (!strategy) {
      return res.status(404).json({ error: '策略不存在' });
    }
    res.json(strategy);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 删除策略
exports.deleteStrategy = async (req, res) => {
  try {
    const result = await strategyService.deleteStrategy(req.params.id);
    if (!result) {
      return res.status(404).json({ error: '策略不存在' });
    }
    res.json({ message: '策略删除成功' });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 回测策略
exports.backtestStrategy = async (req, res) => {
  try {
    const backtestResult = await strategyService.backtestStrategy(req.params.id, req.body);
    res.json(backtestResult);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 启动策略
exports.startStrategy = async (req, res) => {
  try {
    const result = await strategyService.startStrategy(req.params.id);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 停止策略
exports.stopStrategy = async (req, res) => {
  try {
    const result = await strategyService.stopStrategy(req.params.id);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};